package cn.aspect.config;

import cn.aspect.util.AnnotationUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 *
 */
@Aspect
@Slf4j
@Component
public class AspectConfig {


    /**
     * 1.定义切点
     * execution(* cn..*.*(..)) // 匹配 cn 包下及其子孙包下所有类的所有方法
     */
    @Pointcut("execution(* cn..*.*(..))")
    public void annotationPointCut(){}


    /**
     * 2.切点方法执行前运行(前置通知)
     * @param joinPoint
     */
    @Before(value = "annotationPointCut()")
    public void doBefore(JoinPoint joinPoint) throws Exception {
        log.info("前置通知开始----");
        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
           AnnotationUtil.handleSnowflakeId(arg);
        }
        log.info("前置通知结束----");
    }

    /**
     * 3.（环绕通知）
     */
    @Around(value = "annotationPointCut()")
    public Object doAround(ProceedingJoinPoint jointPoint) throws Throwable {
        log.info("around前----");
        // 执行获取返参
        Object proceed = jointPoint.proceed();
        AnnotationUtil.handleDesensitization(proceed);
        log.info("around后----");
        return proceed;
    }

    /**
     * 3.切点方法执行后运行，不管切点方法执行成功还是出现异常(后置通知)
     * @param joinPoint
     */
    @After(value = "annotationPointCut()")
    public void doAfter(JoinPoint joinPoint) {
        log.info("后置通知开始----");
        log.info("后置通知结束----");
    }

}
